package com.senseauto.ah8mediacodec;

import android.text.TextUtils;
import android.util.Log;

import com.senseauto.ah8mediacodec.muxer.MediaMuxerThread;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RecordManager {
    String TAG = "RecordManager";
    String mCurrentVideoPath;
    boolean mIsRecordingVideo = false;
    private static RecordManager instance = new RecordManager();
    public static RecordManager getInstance(){
        return instance;
    }
    public void initVideoFile(int width, int height, String filePath) {
        //之前MediaCodec录制时，先初始化保存录制路径到成员变量里
        String mOutputPath = "";
        if(TextUtils.isEmpty(filePath)) {
            try {
                String path = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".mp4";
                mOutputPath = new File("/storage/emulated/0/Pictures", path).getAbsolutePath();
                mCurrentVideoPath = mOutputPath;
                Log.e(TAG, "文件地址2： " + mOutputPath);
            } catch (final NullPointerException e) {
                throw new RuntimeException("This app has no permission of writing external storage");
            }
        }else{
            mCurrentVideoPath = filePath;
        }
        MediaMuxerThread.init(width, height);
        MediaMuxerThread.setVideoName(mCurrentVideoPath);
    }

    public void startRecord() {
        mIsRecordingVideo = true;
        Log.e(TAG, "MediaMuxerThread.startMuxer()  start recording " + mIsRecordingVideo);
        //2024-3-20 不再用MediaRecorder录制视频，用MediaCodec录制解决视频左右镜像问题
        MediaMuxerThread.startMuxer();
    }

    //点击了停止录视频，或者录制视频到了特定时长调用
    public void stopRecord() {
        mIsRecordingVideo = false;
        //2024-3-20 不再用MediaRecorder录制视频，用MediaCodec录制解决视频左右镜像问题
        //cameraV2Proxy.stopRecorder();
        MediaMuxerThread.stopMuxer();
    }

    public void addVideoFrameData(byte[] data){
        MediaMuxerThread.addVideoFrameData(data);
    }
}
